﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>ControlGetFocus - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The ControlGetFocus function retrieves which control of the target window has keyboard focus, if any." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>ControlGetFocus</h1>

<p>如果有, 则获取目标窗口中具有输入焦点的控件.</p>

<pre class="Syntax">HWND := <span class="func">ControlGetFocus</span>(<span class="optional">WinTitle, WinText, ExcludeTitle, ExcludeText</span>)</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>WinTitle</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a>, <a href="../Concepts.htm#numbers">整数</a>或<a href="../Objects.htm">对象</a></p>
    <p>识别目标窗口的窗口标题或其他条件. 请参阅 <a href="../misc/WinTitle.htm">WinTitle</a>.</p>
  </dd>

  <dt>WinText</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>如果存在, 此参数必须是目标窗口的单个文本元素的子字符串(和内置的 Window Spy 工具显示的一样). 如果 <a href="DetectHiddenText.htm">DetectHiddenText</a> 为 ON, 那么会检测隐藏文本元素.</p>
  </dd>

  <dt>ExcludeTitle</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>标题中含有此参数值的窗口将被排除.</p>
  </dd>

  <dt>ExcludeText</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>文本中含有此参数值的窗口将被排除.</p>
  </dd>

</dl>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
<p>此函数返回具有输入焦点的控件的<a href="../misc/WinTitle.htm#ahk_id">窗口句柄(HWND)</a>.</p>
<p>如果目标窗口的控件都没有焦点, 则返回 0.</p>

<h2 id="Error_Handling">错误处理</h2>
<p>如果在确定目标窗口或控件时出现问题, 则抛出 <a href="Error.htm#TargetError">TargetError</a>.</p>
<p>如果在确定焦点时出现问题, 则抛出 <a href="Error.htm#OSError">OSError</a>.</p>

<h2 id="Remarks">备注</h2>
<p>通过此函数检索的控件是具有键盘焦点的控件, 即, 如果用户键入任何键盘, 将接收击键的控件.</p>
<p>目标窗口必须处于活动状态才能具有焦点控件, 但是即使是活动窗口也可能没有焦点控件.</p>
<p>窗口标题和文本是区分大小写的. 除非 <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> 被打开, 否则不会检测隐藏窗口.</p>

<h2 id="Related">相关</h2>
<p><a href="ControlFocus.htm">ControlFocus</a>, <a href="Control.htm">控件函数</a></p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a class="ex_number" href="#ExBasic"></a> 报告活动窗口的焦点控件的 HWND 和 ClassNN.</p>
<pre>
FocusedHwnd := ControlGetFocus("A")
FocusedClassNN := ControlGetClassNN(FocusedHwnd)
MsgBox 'Control with focus = {Hwnd: ' FocusedHwnd ', ClassNN: "' FocusedClassNN '"}'</pre>
</div>

</body>
</html>